package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName MenuServiceImpl
 * @Author wangqiuxiang
 * @Date 2019/7/5 22:14
 * @Description TODO
 * @Version 1.0
 **/
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

   @Autowired
   private MenuDao menuDao;
    private Integer id;
/**
    *@Author wangqiuxiang
    *@Description //TODO 添加
    *@Date 16:54 2019/7/6
    *@Param [menu]
    *@return void
    **/
    @Override
    public void add(Menu menu) {
        setPathById(menu);
        menuDao.add(menu);
    }
/**
    *@Author wangqiuxiang
    *@Description //TODO 编辑
    *@Date 16:54 2019/7/6
    *@Param [menu]
    *@return void
    **/
    @Override
    public void edit(Menu menu) {
        setPathById(menu);
        menuDao.edit(menu);


    }
/**
    *@Author wangqiuxiang
    *@Description //TODO 根据id查询
    *@Date 16:54 2019/7/6
    *@Param [id]
    *@return com.itheima.pojo.Menu
    **/
    @Override
    public Menu findMenuById(Integer id) {
        return    menuDao.findMenuById(id);
    }

    /**
        *@Author wangqiuxiang
        *@Description //TODO 根据id删除
        *@Date 16:55 2019/7/6
        *@Param [id]
        *@return void
        **/
    @Override
    public void delete(Integer id) {
        Menu menu = menuDao.findMenuById(id);
        menuDao.deleteByParentId(menu.getParentMenuId());
        this.id = id;
        menuDao.delete(id);
    }
    /**
        *@Author wangqiuxiang
        *@Description //TODO 为menu的其他属性赋值
        *@Date 16:55 2019/7/6
        *@Param [menu]
        *@return void
        **/
    public void setPathById(Menu menu){
        if (menu.getId()==null){
        if (StringUtils.isEmpty(menu.getParentMenuId())){
Integer count=menuDao.findMenuCount();
menu.setPath((count+1)+"");
menu.setPriority(count+2);
        }else{
            Menu parentMenu=menuDao.findMenuById(menu.getParentMenuId());
            Integer priority = parentMenu.getPriority();
            Integer count=menuDao.findChildrenCount(menu.getParentMenuId());
            menu.setPriority(count+1);
            menu.setPath("/"+priority+"-"+(count+1));
        }
        }else{
//            之前对象菜单类型
            Menu menu1 = menuDao.findMenuById(menu.getId());
            Integer count=menuDao.findMenuCount();

            if (menu1.getParentMenuId()!=null){
//                证明之前是个二级菜单
                if (menu.getParentMenuId()==null){
//                    现在一级菜单加1
                    menu.setPath((count+1)+"");
                    menu.setPriority(count+2);
                }else{
                    Menu parentMenu=menuDao.findMenuById(menu.getParentMenuId());
                    Integer priority = parentMenu.getPriority();
                  Integer childCount=menuDao.findChildrenCount(menu.getParentMenuId());
//                    现在仍然是二级 总数不变
                    if (menu.getParentMenuId().equals(menu1.getParentMenuId())){
                    menu.setPriority(childCount);
                    menu.setPath("/"+priority+"-"+(count));
                    }else {
//                        父菜单不同数量加1
                        menu.setPriority(childCount+1);
                        menu.setPath("/"+priority+"-"+(childCount+1));
                    }


                }

            }else{
//                之前一级菜单,现在是一级菜单
                if (StringUtils.isEmpty(menu.getParentMenuId())){
                 count=menuDao.findMenuCount();
                menu.setPath((count)+"");
                menu.setPriority(count+1);
            }else{
//                    之前一级菜单,现在是二级菜单
                Menu parentMenu=menuDao.findMenuById(menu.getParentMenuId());
                Integer priority = parentMenu.getPriority();
                 count=menuDao.findChildrenCount(menu.getParentMenuId());
                menu.setPriority(count+1);
                menu.setPath("/"+priority+"-"+(count+1));
            }
            }
        }
    }
/**
    *@Author wangqiuxiang
    *@Description //TODO 分页查询
    *@Date 16:55 2019/7/6
    *@Param [currentPage, pageSize]
    *@return com.itheima.entity.PageResult
    **/
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> menus=menuDao.findPage();
        return new PageResult(menus.getTotal(), menus.getResult());
    }

    /**
        *@Author wangqiuxiang
        *@Description //TODO 获得所有的menu
        *@Date 16:56 2019/7/6
        *@Param []
        *@return java.util.List<com.itheima.pojo.Menu>
        **/
    @Override
    public List<Menu> findAll() {
  List<Menu>menus=menuDao.findAll();
        return menus;
    }

    @Override
    public Integer findParentId(Integer id) {
        return  menuDao.findParentId(id);

    }
}
